/**
 * All rights Reserved, Designed By 信鸥科技
 * project : committee
 * Created by youquer on 17/10/25.
 * 注意：本内容仅限于信鸥科技有限公司内部传阅，禁止外泄以及用于其他的商业目的
 * Description:
 *
 */

import React from 'react';
import PropTypes from 'prop-types';
//Dva
import { Link } from 'dva/router';
import { connect } from 'dva'
//组件antd
import { Popconfirm, message, Card } from 'antd';
//自定义组件
import SmartTable from '@/components/common/Table';
import PageHeaderLayout from '@/layouts/PageHeaderLayout';
//权限
import { checks } from '../../../utils/authority';

//css
import styles from './User.less';

const User = ({dispatch, user}) => {

  /**
   * model参数: patrol
   * type: namespace,
   * dataList: 数据
   * total:总数,
   * page:当前页,
   * pageSize:分页每页数量
   *
   * selectValue:普通选择搜索的值
   * searchValue:普通搜索框的值
   * isAdvance: 是否有高级搜索
   * expand: 高级搜索是否展开
   * advanceTails:高级搜索的值
   * commonSelectData: 普通搜索-搜索项
   * */
  const{ type, total, page, pageSize, dataList, selectValue, searchValue, isAdvance, expand, advanceTails, commonSelectData } = user;

  /**
   * table
   * */
  //表格列的配置描述
  const columns = [
    {
      key: 'index',
      title: '编号',
      dataIndex: 'index',
      render: (text, row) => {
        return <div className={styles.id_width}>{text}</div>
      }
    },
    {
      key: 'realName',
      title: '真实姓名',
      dataIndex: 'realName',
      render: (text, row) =>
      checks('/admin/user/update') ?
        <Link to={{pathname: "/permissions/userEdit", search: '?id=' + row.id}}><div className={styles.desc_width}>{text}</div></Link>
        :
        <div className={styles.desc_width}>{text}</div>
    },
    {
      key: 'apps',
      title: '应用',
      dataIndex: 'apps',
      render: (text) => <div className={styles.desc_width}>{text}</div>
    },
    {
      key: 'lastLoginIp',
      title: '登录IP',
      dataIndex: 'lastLoginIp',
      render: (text) => <div className={styles.desc_width}>{text}</div>
    },
    {
      title: '操作',
      key: 'operation',
      fixed: 'right',
      width: 66,
      render: (record) => (
        checks('/admin/user/del') ?
        <div>
          <Popconfirm title="你确定要删除吗?" onConfirm={() => handleDelete(record.id)} onCancel={cancel}>
            <a href="#">删除</a>
          </Popconfirm>
        </div>
        :
        <div>

        </div>
      )
    }
  ];

  /**
   * 列表删除函数
   * payload: 删除项id
   * delete: 删除方法
   * */
  function handleDelete(id) {
    dispatch({
      type: type + '/delete',
      payload: id
    });
  }
  /**
   * 取消删除
   * */
  function cancel(){
    message.error('您取消了删除!');
  }

  /**
   * scroll: table:横向滚动宽度
   * */
  const scroll = {
    x: 1000
  };

  //data假数据, 正式放到model的真实数据
  const data = [];
  if (dataList){
    for (var i = 0; i < dataList.length; i++){
      var item = dataList[i];
      item["index"] = i + (page - 1) * pageSize + 1;
      data.push(item);
    }
  }


  /**
   * 进入添加页路径
   * */
  var addPathName = "/permissions/userAdd";
  if (!checks('/admin/user/add')){
    addPathName = '';
  }
  /**
   * 高级搜索
   * 高级搜索框动态赋值
   * @type {*[]}
   */
  const advanceSelect = [
    //{
    //  name:"离职类型",
    //  inputName:"departureTypeData",
    //  type:"Select",
    //  selectData:departureTypeData
    //},
    //{
    //  name:"离职原因",
    //  inputName:"departureReasonData",
    //  type:"Select",
    //  selectData:departureReasonData
    //},
  ];

  /**
   * 搜索选择框动态赋值
   * @type {{fistValue: string, secondValue: string}}
   */
  //分页,列表
  const upDateAction= "/dateList";
  //搜索
  const query= "/dateList";

  /**
   * 高级搜索需要接收的值 和普通搜索
   * @type {{selectValue: *普通搜索框, searchValue: *普通输入框, advanceTails: *高级索索值}}
   */
  const advance = {
    advanceSelect,
    commonSelectData,
    selectValue,
    searchValue,
    advanceTails,
    isAdvance,
    expand,
    query
  };


  /**
   * 高级搜索action方法: route
   * */
  const route = "/searchData";


  /**
   * param: 向Table里传入传入数据
   * scroll:滚动
   * columns:表头
   * data:数据
   * total:总共有多少条数据
   * current:当前页数
   * type: model的命名namespace
   * addPathName: 列表添加方法路由
   * */
  const param = {
    columns,
    data,
    total,
    page,
    scroll,
    type,
    addPathName,
    route,
    advance,
    upDateAction,

  };

  return(
    <PageHeaderLayout title="用户列表">
      <Card bordered={false}>
        <SmartTable {...param}/>
      </Card>
    </PageHeaderLayout>
  )
};

export default connect(
  ({user}) => ({user})
)(User);
